package com.ykdl.member.http;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.util.Constants;
import oauth.signpost.http.HttpParameters;
import android.R.integer;
import android.text.TextUtils;
import android.view.View;

import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.Actor_InfoBean;
import com.ykdl.member.kid.beans.AppListBean;
import com.ykdl.member.kid.beans.BodyListBean;
import com.ykdl.member.kid.beans.BreastFeeding;
import com.ykdl.member.kid.beans.DialoguesMessageBean;
import com.ykdl.member.kid.beans.DiaryListBean;
import com.ykdl.member.kid.beans.FavoriteListBean;
import com.ykdl.member.kid.beans.FeedListBean;
import com.ykdl.member.kid.beans.FeedReportListDayBean;
import com.ykdl.member.kid.beans.FeedReportListWeekBean;
import com.ykdl.member.kid.beans.Feeding;
import com.ykdl.member.kid.beans.GetBabyProfileBean;
import com.ykdl.member.kid.beans.GetFileMate;
import com.ykdl.member.kid.beans.GetProfileBean;
import com.ykdl.member.kid.beans.GetUser_ScoreBean;
import com.ykdl.member.kid.beans.Nappy;
import com.ykdl.member.kid.beans.Nursing;
import com.ykdl.member.kid.beans.PersonBean;
import com.ykdl.member.kid.beans.Sleeping;
import com.ykdl.member.kid.beans.StatuResult;
import com.ykdl.member.kid.beans.TopicAndPostListBean;
import com.ykdl.member.kid.beans.TopicListBean;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;


public class Network {
	
	private static final String SERVER = "http://172.16.22.58:5000/lmyrv2";
	private static Network _instance;
	
	private Network() {}

	public static Network getInstance() {
		if (_instance == null) {
			_instance = new Network();
		}
		return _instance;
	}
	
	public void requestBodyRecordList(int cursor, int count, int type, ITag<BodyListBean> tag) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("cursor", String.valueOf(cursor));
		param.put("count", String.valueOf(count));
		param.put("type", String.valueOf(type));
		param.put("acquire_baby_profile", "1");//返回宝贝资料
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.BODY_RECORD_LIST, param, null);
		TaskManager.startHttpRequest(request, tag, BodyListBean.class);
	}
	
	public void addBodyTypeBean(int type, Float value, long record_time,ITag<StatuResult> tag){
		HttpParameters param = new HttpParameters();
		param.put("type", String.valueOf(type));
		param.put("value", String.valueOf(value));
		param.put("record_time", String.valueOf(record_time));
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(KidConfig.BODY_RECORD_ADD, param, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
	public void deleteBodyTypeBean(int id, ITag<StatuResult> tag){
		HttpBaseRequest request = Wxxr.getInstance().deltteRequest(KidConfig.BODY_RECORD_DEL+id, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
	public void deleteFeedBean(long id, ITag<StatuResult> tag){
		HttpBaseRequest request = Wxxr.getInstance().deltteRequest(KidConfig.FEED_RECORD_DEL+id, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
	public void requestFeedRecordList(int cursor, int count, int type, ITag<FeedListBean> tag){
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		map.put("nursing_type", String.valueOf(type));
		map.put("acquire_baby_profile", "1");
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.FEED_RECORD_LIST, map, null);
		TaskManager.startHttpRequest(request, tag, FeedListBean.class);
	}
	
	public void addFeedBean(Nursing nursing, ITag<StatuResult> tag){
		HttpParameters param = new HttpParameters();
		if (nursing instanceof BreastFeeding) {
			BreastFeeding bf = (BreastFeeding) nursing;
			param.put("nursing_type", String.valueOf(bf.getNursing_type()));
			param.put("amount", String.valueOf(200));
			param.put("duration", String.valueOf(bf.getDuration()));
			param.put("vomits", String.valueOf(bf.getVomits()));
			param.put("begin_time", String.valueOf((int)bf.getBegin_time()));
			param.put("end_time", String.valueOf((int)bf.getEnd_time()));
			param.put("breast_type", String.valueOf(bf.getBreast_type()));
		} else if (nursing instanceof Feeding) {
			Feeding fd = (Feeding) nursing;
			param.put("nursing_type", String.valueOf(fd.getNursing_type()));
			param.put("amount", String.valueOf(fd.getAmount()));
			param.put("duration", String.valueOf(fd.getDuration()));
			param.put("vomits", String.valueOf(fd.getDuration()));
			param.put("begin_time", String.valueOf((int)fd.getBegin_time()));
			param.put("end_time", String.valueOf((int)fd.getEnd_time()));
			param.put("feeding_type", String.valueOf(fd.getFeeding_type()));
		} else if (nursing instanceof Sleeping) {
			Sleeping slp = (Sleeping) nursing;
			param.put("nursing_type", String.valueOf(slp.getNursing_type()));
			param.put("duration", String.valueOf(slp.getDuration()));
			param.put("begin_time", String.valueOf((int)slp.getBegin_time()));
			param.put("end_time", String.valueOf((int)slp.getEnd_time()));
		} else if (nursing instanceof Nappy) {
			Nappy na = (Nappy) nursing;
			param.put("nursing_type", String.valueOf(na.getNursing_type()));
			param.put("excrete_type", String.valueOf(na.getExcrete_type()));
			param.put("spill_over", String.valueOf(na.getSpill_over()));
			param.put("shape", String.valueOf(na.getShape()));
			param.put("color", String.valueOf(na.getColor()));
		}
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(KidConfig.FEED_RECORD_ADD, param, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
	public void requestPerson(ITag<PersonBean> tag, int actor_id){
		String url = null;
		if (actor_id == 0) {
			url = KidConfig.PERSON_CENTER;
		} else {
			url = KidConfig.PERSON_CENTER + "/" + actor_id;
		}
		HttpBaseRequest request = Wxxr.getInstance().getBaseRequest(url);
		TaskManager.startHttpRequest(request, tag, PersonBean.class);
	}
	
	public void requestSelfDiary(ITag<DiaryListBean> tag){
		HttpBaseRequest request = Wxxr.getInstance().getBaseRequest(KidConfig.DIARYES_DAYS_SELF);
		TaskManager.startHttpRequest(request, tag, DiaryListBean.class);
	}
	
	public void requestUserDiary(ITag<DiaryListBean> tag, int actor_id){
		HttpBaseRequest request = Wxxr.getInstance().getBaseRequest(KidConfig.DIARYES_DAYS_USER+actor_id);
		TaskManager.startHttpRequest(request, tag, DiaryListBean.class);
	}
	
	public void newDiary(int type, int share, int file_id, ITag<StatuResult> tag){
		HttpParameters param = new HttpParameters();
		param.put("diary_type", String.valueOf(type));
		param.put("is_share", String.valueOf(share));
		param.put("file_ids", String.valueOf(file_id));
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(KidConfig.DIARYES_NEW, param, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
	public void delDiary(long diary_id, ITag<StatuResult> tag){
		HttpParameters param = new HttpParameters();
		String url = KidConfig.DIARYES_NEW + "/" + diary_id;
		HttpBaseRequest request = Wxxr.getInstance().deltteRequest(url, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
	
	public void perfectBabyProfile(String name, float birthday, int blood_type, int born_height, 
			float born_weight, int gender, int avatar_file_id, ITag<StatuResult> tag){
		HttpParameters param = new HttpParameters();
		param.put("baby_name", URLEncoder.encode(name));
		param.put("birthday", String.valueOf((long)birthday));
		param.put("blood_type", String.valueOf(blood_type));
		param.put("born_height", String.valueOf(born_height));
		param.put("born_weight", String.valueOf(born_weight));
		param.put("gender", String.valueOf(gender));
		if (avatar_file_id != 0) {
			param.put("avatar_file_id", String.valueOf(avatar_file_id));
		}
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(KidConfig.PERFECT_BABY_PROFILE, param, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
	public void perfectProfile(String name, float birthday, String region, int gender, String signature, 
			int file_id, ITag<StatuResult> tag){
		HttpParameters param = new HttpParameters();
		param.put("display_name", URLEncoder.encode(name));
		param.put("birthday", String.valueOf((long)birthday));
		param.put("region", URLEncoder.encode(region));
		param.put("gender", String.valueOf(gender));
		param.put("signature", URLEncoder.encode(signature));
		if (file_id != 0) {
			param.put("avatar_file_id", String.valueOf(file_id));
		}
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(KidConfig.CHANGE_PROFILE, param, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	public void newperfectProfile(String name, float birthday, String region, int gender,
			ITag<GetUser_ScoreBean> tag,int status){
		HttpParameters param = new HttpParameters();
		param.put("display_name", URLEncoder.encode(name));
		param.put("address", URLEncoder.encode(region));
		param.put("status", String.valueOf(status));
		switch (status) {
		case 1: // 孕产期
			 param.put("due_date", String.valueOf((long)birthday));
			break;
		case 3: // 6岁以上
			 param.put("user_birthday", String.valueOf((long)birthday));
			break;
		case 2: // 0~6岁
			 param.put("birthday", String.valueOf((long)birthday));
			 param.put("gender", String.valueOf(gender));
			break;

		default:
			break;
		}
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(KidConfig.GET_MYSELF, param, null);
		TaskManager.startHttpRequest(request, tag, GetUser_ScoreBean.class);
	}
	public void perfectProfile(String name, float birthday, String region, int gender,
			int file_id, ITag<GetUser_ScoreBean> tag){
		HttpParameters param = new HttpParameters();
		param.put("display_name", URLEncoder.encode(name));
		param.put("birthday", String.valueOf((long)birthday));
		param.put("region", URLEncoder.encode(region));
		param.put("gender", String.valueOf(gender));
		if (file_id != 0) {
			param.put("avatar_file_id", String.valueOf(file_id));
		}
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(KidConfig.GET_MYSELF, param, null);
		TaskManager.startHttpRequest(request, tag, GetUser_ScoreBean.class);
	}
	
	public void requestFavoriteList(int cursor, int count, ITag<FavoriteListBean> tag){
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.FAVORITE_LIST, map, null);
		TaskManager.startHttpRequest(request, tag, FavoriteListBean.class);
	}
	
	public void requestTopicList(int start_id, int cursor, int count, long post_actor_id, ITag<TopicListBean> tag){
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		map.put("post_actor_id", String.valueOf(post_actor_id));
		map.put("source", "groups");
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.TOPIC_LIST, map, null);
		TaskManager.startHttpRequest(request, tag, TopicListBean.class);
	}
	
	public void requestPostList(int start_id, int cursor, int count, long post_actor_id, ITag<TopicAndPostListBean> tag){
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		map.put("post_actor_id", String.valueOf(post_actor_id));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.POST_LIST, map, null);
		TaskManager.startHttpRequest(request, tag, TopicAndPostListBean.class);
	}
	
	public void getProfile(String id, ITag<GetProfileBean> tag){
		HttpBaseRequest request = Wxxr.getInstance().getBaseRequest(KidConfig.PERFECT_PROFILE+"/"+id);
		TaskManager.startHttpRequest(request, tag, GetProfileBean.class);
	}
	
	public void getBabyProfile(String id, ITag<GetBabyProfileBean> tag){
		HttpBaseRequest request = Wxxr.getInstance().getBaseRequest(KidConfig.PERFECT_BABY_PROFILE+"/"+id);
		TaskManager.startHttpRequest(request, tag, GetBabyProfileBean.class);
	}
	
	public void dialogueMessage(long actor_id, ITag<DialoguesMessageBean> tag){
		//start_id=0&cursor=0&count=50&mark_read=1&actor_ids=6148
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_id", "0");
		map.put("cursor", "0");
		map.put("count", "20");
		map.put("mark_read", "1");
		map.put("actor_ids", String.valueOf(actor_id));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.GET_DIALOGUES_MESSAGES, map, null);
		TaskManager.startHttpRequest(request, tag, DialoguesMessageBean.class);
	}
	
	public void getAppList(ITag<AppListBean> tag){
		Map<String, String> map = new HashMap<String, String>();
		map.put("client_id", Constants.CLIENT_ID);
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.GET_DIALOGUES_MESSAGES, map, null);
		TaskManager.startHttpRequest(request, tag, AppListBean.class);
	}
	
	public void getAvatar(String userid, ITag<GetFileMate> tag){
		HttpBaseRequest request = Wxxr.getInstance().getBaseRequest(KidConfig.GET_AVATAR+userid);
		TaskManager.startHttpRequest(request, tag, GetFileMate.class);
	}
	
	public void getFeedReportListByDay(int cursor, int count, ITag<FeedReportListDayBean> tag){
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.FEED_REPORT_DAY, map, null);
		TaskManager.startHttpRequest(request, tag, FeedReportListDayBean.class);
	}
	
	public void getFeedReportListByWeek(int cursor, int count, ITag<FeedReportListWeekBean> tag){
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(KidConfig.FEED_REPORT_WEEK, map, null);
		TaskManager.startHttpRequest(request, tag, FeedReportListWeekBean.class);
	}
	
	/**
	 * 添加关注
	 * @param userId
	 * @param tag
	 */
	public void addAttention(long userId, ITag<StatuResult> tag){
		HttpParameters param = new HttpParameters();
		param.put("actor_id", String.valueOf(userId));
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(KidConfig.SHIP_FRIENDS, param, null);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
	/**
	 * 取消关注
	 * @param userId
	 * @param tag
	 */
	public void delAttention(long userId, ITag<StatuResult> tag) {
		HttpParameters param = new HttpParameters();
		param.put("actor_id", String.valueOf(userId));
		HttpBaseRequest request = Wxxr.getInstance().deltteRequest(KidConfig.SHIP_FRIENDS, param);
		TaskManager.startHttpRequest(request, tag, StatuResult.class);
	}
	
}
